<template>
  <el-dialog
    :title="!form.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    width="50%"
  >
    <el-form
      :model="form"
      :rules="rules"
      size="mini"
      ref="form"
      @keyup.enter.native="submitForm()"
      label-width="80px"
    >
      <el-form-item label="名称" prop="name">
        <el-input v-model="form.name" placeholder="请输入名称"></el-input>
      </el-form-item>
      <el-form-item label="模板" prop="name">
        <el-select v-model="form.templateCode" placeholder="请选择">
          <el-option label="流金色" value="golden"/>
          <el-option label="红色" value="red"/>
        </el-select>
      </el-form-item>
      <el-row>
        <el-col :span="12">
          <el-form-item label="类型" prop="type">
            <el-select v-model="form.type" placeholder="请选择">
              <el-option
                v-for="dict in dict.type.coupon_type"
                :key="dict.value"
                :label="dict.label"
                :value="parseInt(dict.value)"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="领取类型" prop="receiveType">
            <el-select v-model="form.receiveType" placeholder="请选择">
              <el-option
                v-for="dict in dict.type.coupon_receive_type"
                :key="dict.value"
                :label="dict.label"
                :value="parseInt(dict.value)"/>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="商品" prop="spuIds" v-if="form.type === 1">
        <spu-select v-model="form.spuIds"></spu-select>
      </el-form-item>
      <el-row>
        <el-col :span="12">
          <el-form-item label="总量" prop="num">
            <el-input-number :min="0" v-model="form.num" placeholder="请输入数量"></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="面额" prop="amount">
            <el-input-number :min="0" v-model="form.amount" :precision="2" placeholder="请输入面额"></el-input-number>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="限领张数" prop="limited">
            <el-input-number :min="0" v-model="form.limited" placeholder="请输入限制数量"></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="最低金额" prop="minPoint">
            <el-input-number :min="0" v-model="form.minPoint" placeholder="请输入最低金额"></el-input-number>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="开放时间" prop="startTime">
            <el-date-picker type="datetime" value-format="yyyy-MM-dd HH:mm:ss" v-model="form.startTime" placeholder="请选择"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="结束时间" prop="endTime">
            <el-date-picker type="datetime" value-format="yyyy-MM-dd HH:mm:ss" v-model="form.endTime" placeholder="请选择"></el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="备注" prop="remark">
        <el-input type="textarea" v-model="form.remark" placeholder="备注"></el-input>
      </el-form-item>
      <el-form-item label="模板预览">
        <div :class="form.templateCode === 'red'? 'coupon-box-red' : 'coupon-box-golden' ">
          <div class="left-box">
            <div>
              <span class="amount-prefix">¥</span>
              <span class="amount">{{ form.amount }}</span>
            </div>
          </div>
          <div class="right-box">
            <span class="name">
              {{ form.name }}
            </span>
            <div class="coupon-footer">
              <div class="coupon-date" v-if="form.startTime && form.endTime">
                {{ form.startTime.substr(0,10) }}-{{form.endTime.substr(0,10)}}
              </div>
              <div class="receive-btn">
                点击领取
              </div>
            </div>
          </div>
        </div>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="mini" icon="el-icon-circle-check" type="primary" @click="submitForm()">确定</el-button>
      <el-button size="mini" icon="el-icon-circle-close" @click="visible = false">取消</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {getCoupon, saveCoupon, updateCoupon} from '@/api/coupon/coupon'
import SkuSelect from "@/views/common/skuSelect.vue";
import SpuSelect from "@/views/common/spuSelect.vue";

export default {
  components: {SpuSelect, SkuSelect},
  dicts: ['coupon_type', 'coupon_receive_type'],
  data() {
    return {
      visible: false,
      form: {},
      rules: {
        type: [
          {required: true, message: "请选择类型", trigger: "blur"}
        ],
        spuIds: [
          {required: true, message: "请选择商品", trigger: "blur"}
        ],
        name: [
          {required: true, message: "请输入名称", trigger: "blur"}
        ],
        num: [
          {required: true, message: "请输入总数量", trigger: "blur"}
        ],
        amount: [
          {required: true, message: "请输入面额", trigger: "blur"}
        ],
        limited: [
          {required: true, message: "请输入限制领取数量", trigger: "blur"}
        ],
        minPoint: [
          {required: true, message: "请输入最低使用金额", trigger: "blur"}
        ],
        receiveType: [
          {required: true, message: "请选择领取类型", trigger: "blur"}
        ],
        startTime: [
          {required: true, message: "请选择开始使用时间", trigger: "blur"}
        ],
        endTime: [
          {required: true, message: "请选择结束时间", trigger: "blur"}
        ],
        templateCode: [
          {required: true, message: "请选择模板", trigger: "blur"}
        ]
      }
    };
  },
  created() {
  },
  methods: {
    edit(id) {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["form"].resetFields();
        getCoupon(id).then(res => {
          if (res.success) {
            this.form = res.data
          }
        });
      });
    },
    add() {
      this.form = {}
      this.visible = true
    },
    // 表单提交
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (!this.form.id) {
            saveCoupon(this.form).then(res => {
              if (res.success) {
                this.visible = false;
                this.$emit("ok");
                this.$message.success('新增成功')
              }
            })
          } else {
            updateCoupon(this.form).then(res => {
              if (res.success) {
                this.visible = false;
                this.$emit("ok");
                this.$message.success('修改成功')
              }
            })
          }
        }
      });
    }
  }
};
</script>
<style scoped>
.coupon-box-golden {
  width: 300px;
  height: 100px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
  display: flex;

  .left-box {
    background-color: #E6B872;
    height: 100%;
    width: 110px;
    display: flex;
    justify-content: center;
    align-items: center;

    .amount {
      color: white;
      font-weight: 600;
      font-size: 30px;
    }

    .amount-prefix {
      color: white;
      font-weight: 600;
      font-size: 16px;
    }
  }

  .right-box {
    flex: 1;
    padding: 5px;
    font-size: 12px;
    line-height: 16px;
    color: #000;
    position: relative;

    .coupon-footer {
      position: absolute;
      bottom: 0;
      font-size: 10px;
      display: flex;
      justify-content: space-between;
      margin-bottom: 10px;
      width: 90%; /* 确保父元素宽度为 100% */

      .coupon-date {

      }

      .receive-btn {
        align-self: center;
        background-color: #DFB674;
        height: 20px;
        color: white;
        padding: 0 10px;
        text-align: center;
        line-height: 20px;
        border-radius: 10px;
      }
    }
  }


}

.coupon-box-red {
  width: 300px;
  height: 100px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
  display: flex;

  .left-box {
    background-color: #FF4C42;
    height: 100%;
    width: 110px;
    display: flex;
    justify-content: center;
    align-items: center;

    .amount {
      color: white;
      font-weight: 600;
      font-size: 30px;
    }

    .amount-prefix {
      color: white;
      font-weight: 600;
      font-size: 16px;
    }
  }

  .right-box {
    flex: 1;
    padding: 5px;
    font-size: 12px;
    line-height: 16px;
    color: #000;
    position: relative;

    .coupon-footer {
      position: absolute;
      bottom: 0;
      font-size: 10px;
      display: flex;
      justify-content: space-between;
      margin-bottom: 10px;

      .receive-btn {
        align-self: center;
        background-color: #FF4C42;
        height: 20px;
        color: white;
        padding: 0 10px;
        text-align: center;
        line-height: 20px;
        border-radius: 10px;
      }
    }
  }


}

</style>
